package com.example.tv.codelab;

import com.example.google.tv.leftnavbar.LeftNavBar;
import com.example.google.tv.leftnavbar.LeftNavBarService;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;

import android.app.Activity;
import android.os.Bundle;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.content.Intent;
import android.view.View;


public class VideoSelectionActivity extends DataActivity {
	
	public String[] mCategories = null;
	public LeftNavBar mBar;
    public static final int[] mIcons = {
            R.drawable.icon01,
            R.drawable.icon02, R.drawable.icon03, R.drawable.icon04, R.drawable.icon05
    };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mBar = (LeftNavBarService.instance()).getLeftNavBar((Activity) this);
        mBar.setDisplayUseLogoEnabled(true);

        // Note - I'm showing this so that my UI is pushed down a bit and it
        // does a partial reveal
        // on screen. Thus giving the user an indicator that scrolling is
        // possible.

        mBar.setTitle(R.string.app_name);
        mBar.showOptionsMenu(false);
        // If you want to hide the Title, uncomment the line below.
        // mBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        
        mBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        setContentView(R.layout.main);
    }

    @Override
	void onDataComplete() {
		mCategories = getCategories();

        boolean isfirst = true;
        int iconCounter = mCategories.length - 1;
        for (String name : mCategories) {
	        mBar.addTab(mBar.newTab().setText(name).setIcon(mIcons[iconCounter--])
	                .setTabListener(new ActionBar.TabListener() {

                        @Override
                        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                        }

                        @Override
                        public void onTabSelected(Tab tab, FragmentTransaction ft) {
                            selectCategory(tab.getText().toString());
                        }

                        @Override
                        public void onTabReselected(Tab tab, FragmentTransaction ft) {

                        }
                    }), isfirst);
            isfirst = false;
        }
        mBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
	}
    
    public void selectCategory(final String category) {

        final GridView gridview = (GridView) findViewById(R.id.gridView1);
        gridview.setAdapter(new GridAdapter(this, category));

        
          gridview.setOnItemClickListener(new OnItemClickListener() {
         
            @Override
            public void onItemClick(final AdapterView<?> parent, final View v, final int position,
                    final long id) {
                Video vid = mShows.get(category).get(position);

                final Bundle b = vid.getAsBundle();
                final Intent i = new Intent("com.example.tv.codelab.videoplayer");
                i.putExtra("com.example.tv.codelab.videoplayer", b);
                startActivity(i);
            }
        });
        

    }
}